<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>企业列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../js/layui/css/layui.css" media="all">
    <style type="text/css">
        .layui-table-cell{height:40px;line-height:40px;}
    </style>
</head>
<body >
<!--搜索框-->
<!--<div class="layui-form">-->
<!--    <div class="layui-form-item" style="margin-bottom: 30px;text-align: center;">-->
<!--        <div class="layui-inline">-->
<!--            &lt;!&ndash;<label class="layui-form-label">企业名称</label>&ndash;&gt;-->
<!--            <div class="layui-input-inline">-->
<!--                <input type="text" name="qiyename" placeholder="请输入企业名称" autocomplete="off" class="layui-input" />-->
<!--            </div>-->
<!--        </div>-->
<!--        <div class="layui-inline">-->
<!--            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="search-qiye-submit">-->
<!--                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>-->
<!--            </button>-->
<!--        </div>-->
<!--    </div>-->
<!--</div>-->

<blockquote class="layui-elem-quote quoteBox mod_default_box">
    <form class="layui-form">
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" id="qiyename" name="qiyename" placeholder="请输入企业名称" autocomplete="off" class="layui-input" />
            </div>
            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="search-qiye-submit">
                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">物业</label>
            <div class="layui-input-inline">
                <select name="city" lay-verify="" lay-filter="wuye" id="wuye" lay-search>
                    <option value="">选择物业</option>
                </select>
            </div>
        </div>
    </form>
</blockquote>



<!--数据列表-->
<table class="layui-hide" id="test" lay-filter="qiye-table"></table>

<script type="text/html" id="qiye-toolbar">
    <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
<script type="text/html" id="checkboxTpl">
    <input type="checkbox" name="status" value="{{d.id}}" title="启用" lay-filter="lockDemo" {{ d.status == 1 ? 'checked' : '' }} />
</script>

<script src="../../js/layui/layui.js"></script>
<script src="../../js/layui/layui.all.js"></script>
<script src="../../js/token.js"></script>
<script src="../../js/config.js"></script>

<script>
    layui.use(['table'], function() {
        var $ = layui.$;
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var qiyename = "";
        var wuye = "";
        table.render({
            elem: '#test'
            , id: 'qiye-table-id'//重载需要
            , url: HOST+'qiye/selectqiye?size=5'
            , where: {status:1}
            , title: '企业表'
            , cols: [[
                {type: 'numbers',title: '序号'}
                , {field: 'qyname', align: 'center',width : 165, title: '企业名称'}
                , {field: 'qycode', align: 'center',width : 165, title: '企业编号'}
                , {field: 'fzr', width: 110, align: 'center', title: '负责人'}
                , {field: 'phone', width: 140, align: 'center', title: '联系方式'}
                , {field: 'address',align : 'center', width : 125,title: '公司地址'}
                , {field: 'message',align : 'center',width : 125, title: '公司简介'}
                , {field: 'qyDqtime',align : 'center',width : 165, title: '到期时间'}
                , {field: 'wyName',align : 'center',width : 125, title: '所属物业公司',sort:true}
                , {field: 'logo',align : 'center',title: 'logo',width : 125,templet: function (d) {
                        if (d.logo==null || d.logo=="" || d.logo==undefined){

                        }else{
                            return "<div><img src=\""+HOST+"upload/"+d.logo+"\" onclick=\"previewImgs(this)\"></div>";
                        }
                    }}
                ,{field:'shstatus',align : 'center', title:'审核状态',width:110, templet: function (d) {
                        if(d.shstatus==0)
                            return "未审核";
                        else return "已审核"
                    }, sort:true}
                ,{field:'status',align : 'center', title:'是否启用',width:110, templet: '#checkboxTpl', sort: true , fixed:"right"}
                , {toolbar: "#qiye-toolbar",align : 'center',width:160, title: "操作" , fixed:"right"}
            ]]
            , page: true
            ,page: {
                curr: layui.data("qiye_page").index
            },
            done: (res, curr, count) => {
                layui.data("qiye_page", {
                    key: 'index',
                    value: curr
                });
            }
        });

        //搜索操作
        form.on('submit(search-qiye-submit)', function (data) {
            qiyename = data.field.qiyename;
            table.reload('qiye-table-id', {
                where: {"qyname": data.field.qiyename,"wyid":wuye}
                , page: {
                    curr: 1
                }
            });
            return false;
        });
        // 动态添加物业公司
        $.ajax({
            url:HOST+"wuye/selectwuyestatus" ,
            success:function(res){
                if(res.code == 0) {
                    for(var i in res.data) {
                        $("#wuye").append("<option value='"+res.data[i].id+"'>"+res.data[i].wyname+"</option>")
                    }
                    form.render()
                }
            }
        })
        // 搜索物业
        form.on('select(wuye)', function(data){
            wuye = data.value
            qiyename = $("#qiyename").val()
            table.reload('qiye-table-id', {
                where: { "qyname": qiyename,"wyid":wuye}
                , page: {
                    curr: 1
                }
            });
        });
        //监听启用操作
        form.on('checkbox(lockDemo)', function(obj){
            //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
            var id = this.value;
            var status = obj.elem.checked;
            var zhuangtai;
            if(status){
                zhuangtai=1;
            }else{
                zhuangtai=0;
            }
            var denglu = 1;
            $.ajax({
                url:HOST+"qiye/upStatus?id="+id+"&status="+zhuangtai+"&denglu="+denglu
                ,type:"POST"
                ,contentType: 'application/json'
                ,success:function (data) {
                    if(data.code==0){
                        layer.close(layer.index);
                        layer.msg('操作成功');
                        // table.reload('qiye-table-id');
                        // location.reload(true);
                    }else{
                        layer.msg('没有权限');
                    }
                }
                ,error:function () {
                    console.log("ajax error");
                }
            });
        });

        //表操作  监听操作
        table.on('tool(qiye-table)', function (obj) {
            var data=obj.data;
            var event = obj.event;
            //删除
            if (event === 'del') {
                $.ajax({
                    url: HOST+"staff/getStaffCountByQyId?qyId=" + data.id
                    , type: "GET"
                    , dataType: "json"
                    , success: function (res) {
                        if (res.code == 200) {
                            layer.confirm('该企业下有' + '<span style="color: red">' +  res.data + '</span>' + '个员工 ，确认删除企业吗？', function (index) {
                                layer.prompt({
                                    formType: 0,
                                    value: '',
                                    title: data.qyname + '-请输入删除原因',
                                    btn: ['确定','取消'], //按钮，
                                    btnAlign: 'c',
                                    yes: function (index,layero) {
                                        var content = layero.find(".layui-layer-input").val();
                                        $.ajax({
                                            url:HOST+"qiye/upStatus?id="+data.id+"&status=2&denglu=1&remark="+content
                                            ,type:"POST"
                                            ,contentType: 'application/json'
                                            ,success:function (data) {
                                                if(data.code==0){
                                                    layer.close(layer.index);
                                                    layer.msg('操作成功');
                                                    table.reload('qiye-table-id');
                                                    // location.reload(true);
                                                }else{
                                                    layer.msg('没有权限');
                                                }
                                            }
                                            ,error:function () {
                                                console.log("ajax error");
                                            }
                                        });
                                    }
                                }, function(value,index){
                                    layer.close(index);
                                });
                                /*$.ajax({
                                    url: "/qiye/delqiye?id=" + data.id
                                    , type: "GET"
                                    , dataType: "json"
                                    , success: function (res2) {
                                        if (res2.code == 0) {
                                            layer.msg("删除成功");
                                            table.reload("qiye-table-id");
                                        } else if (res2.code == 1) {
                                            layer.msg(res2.msg);
                                        }else {
                                            layer.msg("拒绝访问");
                                        }
                                    }
                                    , error: function () {
                                        console.log("ajax error");
                                    }
                                });*/
                                layer.close(index);
                            });
                        } else if (res.code == 500) {
                            layer.msg(res.msg);
                        } else {
                            layer.msg("拒绝访问");
                        }
                    }
                    , error: function () {
                        console.log("ajax error");
                    }
                });
            } else if (event === 'edit') {
                layer.open({
                    type:2
                    ,title:"编辑企业"
                    ,skin:'layui-layer-molv'
                    ,area: ['95%','95%']
                    ,content:("updateqiye.html?id="+data.id+"&qyname="+encodeURIComponent(data.qyname)+"&qycode="+data.qycode+"&fzr="+data.fzr+"&phone="+encodeURIComponent(data.phone)+"&address="+encodeURIComponent(data.address)+"&message="+encodeURIComponent(data.message)+"&logo="+data.logo+"&wyName="+encodeURIComponent(data.wyName)+"&wyID="+data.wyID+"&qyDqtime="+data.qyDqtime)  //js获取url参数
                });
            }
        });
    });
</script>

<script>
    function previewImgs(obj) {
        var img = new Image();
        img.src = obj.src;
        //var height = img.height + 50; // 原图片大小
        //var width = img.width; //原图片大小
        var imgHtml = "<img src='" + obj.src + "' width='800px' height='500px'/>";
        //弹出层
        layer.open({
            type: 1,
            shade: 0.8,
            offset: 'auto',
            area: [800 + 'px', 550 + 'px'],// area: [width + 'px',height+'px'],  //原图显示
            shadeClose: true,
            scrollbar: false,
            title: "图片预览", //不显示标题
            content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: function () {
                //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
            }
        });
    }
</script>
</body>
</html>